#pragma once

#include <vector>
#include "h2base.h"
#include "win/win_base.h"


#define ALIGN_LEFT              0
#define ALIGN_TOP               1
#define ALIGN_RIGHT             2
#define ALIGN_BOTTOM            3

#define CORNER_TOP_LEFT         0
#define CORNER_TOP_RIGHT        1
#define CORNER_BOTTOM_RIGHT     2
#define CORNER_BOTTOM_LEFT      3

#define MAX_WIDTH               0x7F000000
#define MAX_HEIGHT              0x7F000000

#define PIR_MAINWND             0x00000000
#define PIR_SIDEWND             0x10000000
#define PIR_TOOLBAR             0x20000000


class win_tile : public win_base
{
friend class            win_layout;
friend class            win_region;

protected:
    int                 priority;
    int                 max_width;
    int                 max_height;
    int                 align;
    int                 corner;

public:
    win_tile() : priority(0), align(ALIGN_TOP), corner(CORNER_TOP_LEFT), max_width(0), max_height(0) {}
    ~win_tile()                                 {}
};


class win_region
{
private:
    data_vector<RECT>   _blocks;

public:
    win_region()                            {}
    ~win_region()                           {}

    void                reset(RECT& rect)   { _blocks.clear(); _blocks.push_back(rect); }
    void                remove(RECT& rect);
    void                add_rect(RECT& rc);
    void                add_notch_rect(RECT& rcSrc, RECT& rcNotch);
    bool                find_align_rect(int align, RECT* rc);

    bool                cover_rect(RECT& rc, RECT& rcRef);
    bool                overlap_rect(RECT& rc, RECT& rcRef);
    bool                merge_rect(RECT* rc, RECT& rcRef);

    void                debug_info();
};



class win_layout
{
private:
    win_region              _region;
    data_vector<win_tile*>  _tiles;

public:
    win_layout()                            {}
    ~win_layout()                           {}

    void                resize(int width, int height);
    bool                accommodate(win_tile* t);
    void                add_tile(win_tile *t);

};


class win_frame : public win_base, public win_layout
{
public:
    win_frame();
    ~win_frame();

protected:
    virtual LRESULT OnEraseBackground(HDC hDC) {return 0;}
	virtual LRESULT OnSize(DWORD dwFlags, WORD width, WORD height);

private:
    int                     _drag;          // 
    POINT                   _ptDragOffset;  // The offset of drag point to the top-left conner
};
